<?php
if (!defined('SHARE_VERSION')) exit('No direct script access allowed');
// +----------------------------------------------------------------------
// | 项目说明：
// +----------------------------------------------------------------------
// | 本页功能：基础类，用于整个系统的一些初始化工作
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.itye.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 吴锋(7462679@qq.com)
// +----------------------------------------------------------------------
class Base {
	protected $db;
	protected $tpl;
	protected $app;
	protected $controller;
	protected $operate;
	protected $config;
	protected $cache;
	
	public function __construct() {
		$this->config                = $GLOBALS['config'];
		$this->db                    = new MySQL($this->config['db']);
		$this->controller            = $this->_getController();
		$this->operate               = $this->_getOperate();
		// 载入缓存类
		$this->cache                 = new Cache;
		// 载入模板类
		$this->tpl                   = new Template;
		$this->tpl->templateDir      = ROOT_PATH .'Template/'.APP_NAME ;
		$this->tpl->templateCompiled = ROOT_PATH . 'Public/Compiled/'.APP_NAME;
		define('DIR', $this->config['dir']);
		define('PUB', DIR.'Public/');
		define('IMG', PUB.'Images/');
	}

	public function init(){
		chkAppDir();	// 检测app的目录
		// 当前要运行的
		$className = $this->controller."Controller";
		if (class_exists($className)) {
			$className = new $className();
		} else {
			showError('无法加载此类','无法加载此类！<br />'.$className);
		}
		if (method_exists($className, $this->operate)) {
			$className->{$this->operate}();
		} else {
			showError('不存在此方法','不存在此方法！'.$this->controller.":".$this->operate);
		}
	}

	// 取得controller
	private function _getController(){
		$Controller 		= getGpc('c', 'string', 'G');
		if (empty($Controller)) {
			$Controller 	= 'index';
		}
		$Controller 		= replaceUrlStyle($Controller);
		return $Controller;
	}
	// 取得operate
	private function _getOperate(){
		$Operate			= getGpc('a', 'string', 'G');
		if (empty($Operate)) {
			$Operate 		= 'index';
		}
		$Operate 			= replaceUrlStyle($Operate);
		return $Operate;
	}
	
	protected function display($filename='') {
		return $this->tpl->display($filename);
	}
	
	protected function alert($msg, $url='', $type = 1) {
		include $this->display('alert');
		exit();
	}

	protected function msg($msg="", $status=0){
		$result['status']	= $status;
		$result['msg']		= $msg;
		echo json_encode($result);
		exit();
	}
}
?>